package com.liu.stockmarket.ui.kline

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.webkit.WebViewClient
import androidx.core.os.bundleOf
import androidx.lifecycle.ViewModelProvider
import com.liu.stockmarket.BaseActivity
import com.liu.stockmarket.StockMarketApplication
import com.liu.stockmarket.databinding.ActivityKlineBinding
import com.liu.stockmarket.utils.start

class KlineActivity : BaseActivity() {
    private lateinit var binding: ActivityKlineBinding

    private lateinit var viewModel: KlineViewModel
    @SuppressLint("SetJavaScriptEnabled")
    override fun initView(savedInstanceState: Bundle?) {
        binding = ActivityKlineBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setToolbar(binding.toolbar.toolbar, true)
        val stockMarketApplication = application as StockMarketApplication
        viewModel = ViewModelProvider(
            this,
            KlineViewModel.KlineViewModelFactory(stockMarketApplication.easyMoneyRepository)
        ).get(KlineViewModel::class.java)

        var code: String? = "";
        var name: String? = "";
        var type = "hs"

        intent.extras?.let {
            code = it.getString(STOCK_CODE_KEY)
            name = it.getString(STOCK_NAME_KEY)
            type = it.getString(STOCK_TYPE_KEY,"hs")
        }
        title = name ?: ""
        setSubTitle(code ?: "")
        binding.swipeLayout.setColorSchemeColors(Color.argb(100, 255, 152, 0))
        with(binding.web) {
            settings.javaScriptEnabled = true
            settings.domStorageEnabled =true
            webViewClient = WebViewClient()
//            loadUrl("file:///android_asset/line.html")
            if (code?.contains(".") == false) {
                loadUrl("http://fund.eastmoney.com/${code}.html?spm=aladin")
            }else{
                loadUrl("file:///android_asset/demo.html?code=${code}&type=${type}")
            }


            visibility = View.VISIBLE
        }


    }

    override fun listener() {
        binding.swipeLayout.setOnRefreshListener {
            binding.web.reload()
            binding.swipeLayout.isRefreshing = false
        }
    }

    override fun observer() {
        viewModel.stockInfo.observe(this) {
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.web.destroy()
    }

    companion object {
        const val STOCK_CODE_KEY = "code"
        const val STOCK_NAME_KEY = "name"
        const val STOCK_TYPE_KEY = "type"

        /**
         *  * @param  type   hs=沪深  us=美股  hk=港股
         */
        fun start(context: Context, code: String, name: String, type: String) {
            context.start(
                bundle = bundleOf(
                    STOCK_CODE_KEY to code,
                    STOCK_NAME_KEY to name,
                    STOCK_TYPE_KEY to type,
                ), KlineActivity::class.java
            )
        }
    }

}